/* eslint no-unused-vars:0 */
import React from 'react';
import PubSub from 'pubsub-js';
import 'antd/dist/antd.css';

import { Table, Menu } from 'antd';

import { Link } from 'react-router-dom';

export default class WorkBrieging extends React.Component {
    state = {
        module: [],
        data: [],
        columns: [
            {
                title: '舆情分析报告',
                key: 'originalName',
                inputType: 'input',
                dataIndex: 'originalName',
                editable: true
            },
            {
                title: '作者',
                key: 'authorName',
                inputType: 'input',
                dataIndex: 'authorName',
                editable: true
            },
            {
                title: '预览',
                key: 'reportFile',
                render: (text, record) => {
                    return (
                        <div>
                            <a onClick={this.download.bind(text, record)}>
                                下载文档
                            </a>
                        </div>
                    );
                }
            }
        ],
        page: 1,
        size: 5,
        total: 0
    };

    constructor(props) {
        super(props);
    }

    componentDidMount() {
        this.getData(1);
    }

    getData = (page) => {
        this.$post('workBriefing/getWorkBriefing', {
            page: page,
            size: this.state.size
        })
            .then((res) => {
                if (res) {
                    var data = res.obj;
                    for (var i = 0; i < data.length; i++) {
                        data[i]['key'] = data[i].id;
                    }
                    this.setState({
                        data: data,
                        total: res.maxCount
                    });
                } else this.$showNotification('error', '获取数据失败');
            })
            .catch((e) => {
                console.log(e);
            });
    };

    pageChange = (page, size) => {
        this.setState({
            page: page
        });
        this.getData(page);
    };

    download = (data, record) => {
        this.$post('/workBriefing/download', data.saveName)
            .then((res) => {
                // 组装a标签
                let elink = document.createElement('a');

                // // 设置下载文件名
                elink.download = res.saveName;
                elink.style.display = 'none';

                elink.href = res.filePath;
                elink.target = '_Blank';
                document.body.appendChild(elink);
                elink.click();
                document.body.removeChild(elink);
            })
            .catch((e) => {
                console.log(e);
            });
    };

    render() {
        return (
            <div className="body">
                <Table
                    columns={this.state.columns}
                    dataSource={this.state.data}
                    pagination={{
                        current: this.state.page,
                        pageSize: this.state.size,
                        total: this.state.total,
                        onChange: this.pageChange
                    }}
                />
            </div>
        );
    }
}
